﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors.Mask;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.CodeDom;

namespace ProgNetExtensions.DataProviderTools
{
    [DesignerSerializer(typeof(DataProviderColumnSerializer), typeof(CodeDomSerializer))]
    public class DataProviderColumn :ICloneable
    {
        [DefaultValue(null)]
        public string Expression
        {
            get;
            set;
        }

        public string ColumnName
        {
            get;
            set;
        }

        [DefaultValue(null)]
        public string Caption
        {
            get;
            set;
        }

        [DefaultValue(true)]
        public bool Visible
        {
            get;
            set;
        }

        [DefaultValue(50)]
        public int Length
        {
            get;
            set;
        }

        [DefaultValue(DataProviderColumnType.Text)]
        public DataProviderColumnType ColumnType
        {
            get;
            set;
        }

        [DefaultValue(DataProviderColumnFilterType.BeginsWith)]
        public DataProviderColumnFilterType FilterType
        {
            get;
            set;
        }
        
        [DefaultValue(CharacterCasing.Normal)]
        public CharacterCasing Casing
        {
            get;
            set;
        }
         
        [DefaultValue(false)]
        public bool CaseSensitiveSearch
        {
            get;
            set;
        }

        [DefaultValue(true)]
        public bool IsSearchField
        {
            get;
            set;
        }

        [DefaultValue(MaskType.None)]
        public MaskType MaskType
        {
            get;
            set;
        }

        [DefaultValue(null)]
        public string MaskEdit
        {
            get;
            set;
        }
        
        #region CTOR
        public DataProviderColumn()
        {
            Visible = true;
            IsSearchField = true;
            Length = 50;
            ColumnType = DataProviderColumnType.Text;
            FilterType = DataProviderColumnFilterType.BeginsWith;
            Casing = CharacterCasing.Normal;
        }
        #endregion

        #region ICloneable Members

        public object Clone()
        {
            return this.CloneObj();
        }

        #endregion


        public override string ToString()
        {
            return ColumnName;
        }
    }

    public enum DataProviderColumnType
    {
        Text,
        CheckBox,
        LookUp,
        Date,
        Number
    }

    public enum DataProviderColumnFilterType
    {
        /// <summary>
        /// COLUMN Like 'XXX%'
        /// </summary>
        BeginsWith,
        /// <summary>
        /// COLUMN = 'XXX'
        /// </summary>
        Equals,
        /// <summary>
        /// COLUMN LIKE '%XXX%'
        /// </summary>
        Contains,
        /// <summary>
        /// COLUMN > 'XXX'
        /// </summary>
        Bigger,
        /// <summary>
        /// COLUMN >= 'XXX'
        /// </summary>
        BiggerOrEqual,
        /// <summary>
        /// <para>COLUMN &lt; 'XXX' </para>
        /// </summary>
        Smaller,
        /// <summary>
        /// <para>COLUMN &lt;= 'XXX' </para>
        /// </summary>
        SmallerOrEqual,
    }

    public class DataProviderColumnSerializer : CodeDomSerializer
    {
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
            object codeObject = base.Serialize(manager, value);

            try
            {
                // Get the container with statements
                CodeStatementCollection statements = (CodeStatementCollection)codeObject;
                if (statements.Count > 1)
                {
                    //statements.Insert(0, new CodeCommentStatement(""));
                    statements.Insert(0, new CodeCommentStatement("[" + (value as DataProviderColumn).ColumnName + "]"));
                    //statements.Insert(0, new CodeCommentStatement(""));
                }
            }
            catch { }
            return codeObject;
        }
        
        public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
        {
           
            return base.Deserialize(manager, codeObject);
        }
    
    }
}
